using Android.Graphics;

namespace KinonekoSoftware.Foundation.Platform.Graphics
{
    public class ImageProcessorImpl : IPlatformImageProcessor
    {
        public void Release()
        {
            
        }
        
        public void Resize(byte[] buffer, int w, int h, out byte[] array)
        {
            var backend = BitmapFactory.DecodeByteArray(buffer, 0, buffer.Length);
            var resized = Bitmap.CreateScaledBitmap(backend, w, h, false);

            backend.Recycle();
            backend.Dispose();

            var ms = new MemoryStream(w * h * 4);

            //
            //
            resized.Compress(Bitmap.CompressFormat.Png, 100, ms);

            resized.Recycle();
            resized.Dispose();

            array = ms.ToArray();
            ms.Dispose();
        }

        public void Crop(byte[] buffer, int x, int y, int w, int h, out byte[] array)
        {
            var backend = BitmapFactory.DecodeByteArray(buffer, 0, buffer.Length);
            var cropped = Bitmap.CreateBitmap(backend, x, y, w, h);

            backend.Recycle();
            backend.Dispose();

            var ms = new MemoryStream(w * h * 4);

            //
            //
            cropped.Compress(Bitmap.CompressFormat.Png, 100, ms);

            cropped.Recycle();
            cropped.Dispose();

            array = ms.ToArray();
            ms.Dispose();

        }
    }
}